package com.rul.mars.product.controller;

import com.rul.mars.common.api.CommonResult;
import com.rul.mars.mbg.domain.ProductParam;
import com.rul.mars.product.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 商家商品服务接口
 *
 * @author RuL
 */
@Api(tags = "MerchantProductController", description = "商家商品服务接口")
@RestController
@RequestMapping("/merchant/product")
public class MerchantProductController {

    @Autowired
    private ProductService productService;

    /**
     * 创建商品
     *
     * @param param 创建商品所需参数
     * @return 创建结果
     */
    @ApiOperation("创建商品")
    @PostMapping("/create")
    public CommonResult createProduct(@RequestBody ProductParam param) {
        productService.createProduct(param);
        return CommonResult.success();
    }

    /**
     * 根据商品ID获取商品可修改信息
     *
     * @param id 商品ID
     * @return 商品详情
     */
    @ApiOperation("获取商品可修改信息")
    @GetMapping("/info/{id}")
    public CommonResult<ProductParam> infoProduct(@PathVariable(value = "id") Long id) {
        ProductParam productParam = productService.infoProduct(id);
        return CommonResult.success(productParam);
    }

    /**
     * 修改商品信息
     *
     * @param param 商品信息
     * @return 修改状态
     */
    @ApiOperation("修改商品信息")
    @PostMapping("/update")
    public CommonResult updateProduct(@RequestBody ProductParam param) {
        productService.updateProduct(param);
        return CommonResult.success();
    }

    /**
     * 修改商品上下架状态
     *
     * @param id            商品ID
     * @param publishStatus 上下架状态（0：下架 1：上架）
     * @return 修改结果
     */
    @ApiOperation("修改商品上下架状态")
    @PostMapping("/updatePublish")
    public CommonResult updatePublishStatus(@RequestParam("id") Long id,
                                            @RequestParam("publishStatus") Byte publishStatus) {
        boolean success = productService.updatePublishStatus(id, publishStatus);
        if (success) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    /**
     * 删除商品
     *
     * @param id 商品ID
     * @return 删除结果
     */
    @ApiOperation("删除商品")
    @PostMapping("/delete")
    public CommonResult deleteProduct(@RequestParam("id") Long id) {
        boolean success = productService.deleteProduct(id);
        if (success) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }
}
